package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.model.DriverGrade;
import com.ybkj.daijia.server.service.DriverGradeService;
import com.ybkj.daijia.server.service.DriverService;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("drivergrade")
public class DriverGradeController {

    @Autowired
    private DriverGradeService driverGradeService;
    @Autowired
    private DriverService driverService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list() {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("drivergrade")) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("drivergrade/list");
            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "listdata", method = RequestMethod.GET)
    @ResponseBody
    public Page<DriverGrade> driverApplyDatas() {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("drivergrade")) {
            Page<DriverGrade> queryAll = driverGradeService.queryAll();
            return queryAll;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "crateGrade", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> crateGrade(
        @RequestParam("name") String name,
        @RequestParam("grade") Integer grade) {

        Subject subject = SecurityUtils.getSubject();

        Map<String, Object> map = new HashMap<String, Object>();
        if (subject.isPermitted("drivergrade")) {
            try {

                DriverGrade driverGrade = driverGradeService.findOneByName(name);
                if (null != driverGrade) {
                    map.put("success", false);
                    map.put("message", "已存在相同名称等级");
                    return map;
                }

                driverGradeService.createGrade(name, grade);
                map.put("success", true);
                map.put("message", "创建成功");
                return map;
            } catch (Exception e) {
                e.printStackTrace();
                map.put("success", false);
                map.put("message", e.getMessage());
                return map;
            }
        } else {
            map.put("success", false);
            map.put("message", "您没有该权限");
            return map;
        }
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> delete(
        @RequestParam("id") Long id) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("drivergrade")) {
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                driverGradeService.delete(id);
                driverService.updateGrad2NullByGrade(id);
                map.put("success", true);
                map.put("message", "创建成功");
            } catch (Exception e) {
                e.printStackTrace();
                map.put("success", false);
                map.put("message", e.getMessage());
            }
            return map;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> update(
        @RequestParam("id") Long id,
        @RequestParam(required = false, value = "name") String name,
        @RequestParam(required = false, value = "grade") Integer grade) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("drivergrade")) {
            Map<String, Object> map = new HashMap<String, Object>();
            try {
                DriverGrade dg = driverGradeService.findOne(id);

                if (dg == null) {
                    map.put("success", false);
                    map.put("message", "服务人员等级不存在");
                    return map;
                }

                DriverGrade dbGrade = driverGradeService.findOneByName(name);
                if (null != dbGrade && !dbGrade.getId().equals(dg.getId())) {
                    map.put("success", false);
                    map.put("message", "已存在相同名称等级");
                    return map;
                }

                dg.setName(StringUtils.isNotBlank(name) ? name : dg.getName());
                dg.setGrade(grade != null ? grade : dg.getGrade());
                driverGradeService.update(dg);
                map.put("success", true);
                map.put("message", "修改成功");
                return map;

            } catch (Exception e) {
                e.printStackTrace();
                map.put("success", false);
                map.put("message", e.getMessage());
                return map;
            }
        } else {
            throw new NoPermissionException();
        }

    }


}
